/**
 * Copyright 2005-2012 Flouny.Caesar.com
 * All rights reserved.
 * 
 * @project
 * @author Brin
 * @version 1.0
 * @data 2013-04-01
 */
package com.brin.core.base.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.brin.core.model.IPOJO;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * Base DAO基础类
 * @author Brin
 * 
 * DAO基础方法集：C.R.U.D.T.Q.S.
 * C is create
 * R is read
 * U is update
 * D is delete
 * T is count
 * Q is query
 * S is search
 * 
 * 子类应当尽可能重用上述方法
 *
 * @param <T>
 */
public abstract class BaseDao<T> extends SqlMapClientDaoSupport {

	protected String namespace;
	
	/**
	 * 新增
	 * @param pojo
	 * @return
	 */
	protected Object insert(IPOJO pojo) {
		
		return insert("create", pojo);
	}
	
	/**
	 * 新增
	 * @param sqlId
	 * @param pojo
	 * @return
	 */
	protected Object insert(String sqlId, IPOJO pojo) {
		
		return (Object) getSqlMapClientTemplate().insert(namespace + sqlId, pojo);
	}
	
	/**
	 * 修改
	 * @param pojo
	 */
	protected int update(IPOJO pojo) {
		
		return update("update", pojo);
	}
	
	/**
	 * 修改
	 * @param sqlId
	 * @param pojo
	 * @return
	 */
	protected int update(String sqlId, IPOJO pojo) {
		
		return getSqlMapClientTemplate().update(namespace + sqlId, pojo);
	}
	
	/**
	 * 删除数据
	 * @param pojo
	 * @return
	 */
	protected int delete(IPOJO pojo) {
		
		return delete("delete", pojo);
	}
	
	/**
	 * 删除数据
	 * @param sqlId
	 * @param pojo
	 * @return
	 */
	protected int delete(String sqlId, IPOJO pojo) {
		
		return getSqlMapClientTemplate().delete(namespace + sqlId, pojo);
	}
	
	/**
	 * 查询总数
	 * @param sqlId
	 * @param data
	 * @return
	 */
	protected int count(String sqlId, Object data) {
		
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace + sqlId, data);
	}
	
	/**
	 * 返回单条对象
	 * @param sqlId
	 * @param data
	 * @return
	 */
	protected Object queryForObject(String sqlId, Object data) {
		
		return getSqlMapClientTemplate().queryForObject(namespace + sqlId, data);
	}
	
	/**
	 * 返回列表(不带分页)
	 * @param sqlId
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected List queryForList(String sqlId, Object data) {
		
		return getSqlMapClientTemplate().queryForList(sqlId, data);
	}
	
	/**
	 * 返回列表(带分页)
	 * @param sqlId
	 * @param data
	 * @param object
	 * @return
	 */
//	@Deprecated
//	@SuppressWarnings("unchecked")
//	protected List<T> searchWithPage(String sqlId, Object data, PageObject object) {
//		Map<Object, Object> iMap = null;
//		if (data == null) {
//			iMap = new HashMap<Object, Object>();
//		} else if (data instanceof IDataObject) {
//			iMap = MapUtil.populateMap(new HashMap<Object, Object>(), data);
//		} else if (data instanceof Map) {
//			iMap = (Map<Object, Object>) data;
//		} else {
//			throw new RuntimeException("Input Object Type Error!");
//		}
//		
//		return (List<T>) getSqlMapClientTemplate().queryForList(namespace + sqlId, MapUtil.populateMap(iMap, object));
//	}
	
	/**
	 * 返回Grid
	 * @param sqlId
	 * @param gridReq
	 * @return
	 */
//	@Deprecated
//	protected List<T> searchWithGrid(String sqlId, GridAjaxRequest gridReq) {
//		
//		return searchWithGrid(sqlId, gridReq, null);
//	}
	
	/**
	 * 返回Grid
	 * @param sqlId
	 * @param gridReq
	 * @param moreData
	 * @return
	 */
//	@Deprecated
//	@SuppressWarnings("unchecked")
//	protected List<T> searchWithGrid(String sqlId, GridAjaxRequest gridReq, Object moreData) {
//		Map<Object, Object> map = null;
//		if (moreData == null) {
//			map = new HashMap<Object, Object>();
//		} else if (moreData instanceof IDataObject) {
//			map = MapUtil.populateMap(new HashMap<Object, Object>(), moreData);
//		} else if (moreData instanceof Map) {
//			map = (Map<Object, Object>)moreData;
//		} else {
//			throw new RuntimeException("Input Object Type Error!");
//		}
//		
//		Map<Object, Object> iMap = MapUtil.populateMap(map, gridReq);
//		
//		return getSqlMapClientTemplate().queryForList(namespace + sqlId, iMap);
//	}
	
	public void setNamespace(String namespace) {
		this.namespace = namespace + ".";
	}
	
	@Autowired
	public void setSqlMapClientForAutowire(SqlMapClient sqlMapClient) {
		super.setSqlMapClient(sqlMapClient);
	}
}
